package com.example.controller;

import com.example.common.Result;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.LevelThree;
import com.example.service.LevelThreeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 三级机构前端操作接口
 **/
@RestController
@RequestMapping("/levelThree")
public class LevelThreeController {

    @Resource
    private LevelThreeService levelthreeService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody LevelThree levelthree) {
        levelthreeService.add(levelthree);
        return Result.success(levelthree);
    }

   /* @PostMapping("/add")
    public Result add(@RequestBody LevelThree levelThree) {
        try {
            LevelThree newLevelThree = levelthreeService.addAndGet(levelThree);
            return Result.success(newLevelThree); // 使用带数据的success方法
        } catch (Exception e) {
            // 处理异常，返回错误信息
            return Result.error(ResultCodeEnum.SYSTEM_ERROR.code, e.getMessage());
        }
    }*/

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        levelthreeService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        levelthreeService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody LevelThree levelthree) {
        levelthreeService.updateById(levelthree);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        LevelThree levelThree = levelthreeService.selectById(id);
        System.out.println(levelThree);  // 打印值
        System.out.println(levelThree.getClass().getName());// 打印类型
        System.out.println("-----------------------------------------------------");
        return Result.success(levelThree);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(LevelThree levelthree ) {
        List<LevelThree> list = levelthreeService.selectAll(levelthree);
        return Result.success(list);
    }
    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(LevelThree levelthree,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<LevelThree> page = levelthreeService.selectPage(levelthree, pageNum, pageSize);
        return Result.success(page);
    }

}